﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="BoundingBoxExtensions.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2010 
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.Core.
//
//   starLiGHT.Core is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   starLiGHT.Core is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public License
//   along with starLiGHT.Core. If not, see http://www.gnu.org/licenses/.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.Core are available on request.
// </license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 620                   $:  Revision of last commit *
// * $Author:: unknown            $:  Author of last commit   *
// * $Date:: 2010-12-07 15:23:58 #$:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;

    #endregion

    /// <summary>
    /// Extension methods for the BoundingBox
    /// </summary>
    public static class BoundingBoxExtensions
    {
#if !ZUNE
        private static BasicEffect effect;
        private static VertexDeclaration vertDecl;
        
        private static VertexPositionColor[] verts = new VertexPositionColor[8];
        
#if !MONO
        private static int[] indices = new int[]
#else
		private static ushort[] indices = new ushort[]
#endif
        {        
            0, 1,
            1, 2,
            2, 3,
            3, 0,
            0, 4,
            1, 5,
            2, 6,
            3, 7,
            4, 5,
            5, 6,
            6, 7,
            7, 4,
        };

        public static void Render(this BoundingBox box, GraphicsDevice graphicsDevice, ICamera3D camera, Color color)
        {
            if (effect == null)
            {
#if !XNA4
                vertDecl = new VertexDeclaration(graphicsDevice, VertexPositionColor.VertexElements);
                effect = new BasicEffect(graphicsDevice, null);
#else
                vertDecl = VertexPositionColor.VertexDeclaration;
                effect = new BasicEffect(graphicsDevice);
#endif
                effect.VertexColorEnabled = true;
                effect.LightingEnabled = false;
            }

            Vector3[] corners = box.GetCorners();
            for (int i = 0; i < 8; i++)
            {
                verts[i].Position = corners[i];
                verts[i].Color = color;
            }

#if !XNA4
            graphicsDevice.VertexDeclaration = vertDecl;
#endif

            effect.View = camera.View;
            effect.Projection = camera.Projection;

#if !XNA4
            effect.Begin();
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Begin();

                graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.LineList, verts, 0, 8, indices, 0, indices.Length / 2);

                pass.End();
            }

            effect.End();
#else
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.LineList, verts, 0, 8, indices, 0, indices.Length / 2);
            }
#endif
        }
#endif

        public static BoundingBox TransformAffine(this BoundingBox b, Matrix m)
        {
            //// ////////TODO: check for extent finite

            Vector3 centre = b.getCenter();
            Vector3 halfSize = b.getHalfSize();

            Vector3 newCentre = m.TransformAffine(centre);
            Vector3 newHalfSize = new Vector3(
                                              (Math.Abs(m.M11) * halfSize.X) + (Math.Abs(m.M12) * halfSize.Y) + (Math.Abs(m.M13) * halfSize.Z),
                                              (Math.Abs(m.M21) * halfSize.X) + (Math.Abs(m.M22) * halfSize.Y) + (Math.Abs(m.M23) * halfSize.Z),
                                              (Math.Abs(m.M21) * halfSize.X) + (Math.Abs(m.M22) * halfSize.Y) + (Math.Abs(m.M23) * halfSize.Z));

            b.Min = newCentre - newHalfSize;
            b.Max = newCentre + newHalfSize;

            return b;
        }

        public static float BoundingRadius(this BoundingBox b)
        {
            Vector3 magnitude = b.Max;
            magnitude = magnitude.Ceil(-b.Max);
            magnitude = magnitude.Ceil(b.Min);
            magnitude = magnitude.Ceil(-b.Min);

            return magnitude.Length();
        }

        public static BoundingBox setNull(this BoundingBox b)
        {
            b.Min = Vector3.Zero;
            b.Max = Vector3.Zero;

            return b;
        }

        public static bool IsNull(this BoundingBox b)
        {
            return b.Min == Vector3.Zero && b.Max == Vector3.Zero;
        }

        public static bool IsInfinite(this BoundingBox b)
        {
            return float.IsInfinity(b.Min.X) &&
                   float.IsInfinity(b.Min.Y) &&
                   float.IsInfinity(b.Min.Z) &&
                   float.IsInfinity(b.Max.X) &&
                   float.IsInfinity(b.Max.Y) &&
                   float.IsInfinity(b.Max.Z);
        }

        public static Vector3 getCenter(this BoundingBox b)
        {
            return new Vector3(
                               (b.Max.X + b.Min.X) * 0.5f,
                               (b.Max.Y + b.Min.Y) * 0.5f,
                               (b.Max.Z + b.Min.Z) * 0.5f);
        }

        public static Vector3 getSize(this BoundingBox b)
        {
            if (b.IsNull()) 
            {
                return Vector3.Zero;
            }

            if (b.IsInfinite())
            {
                return new Vector3(float.PositiveInfinity);
            }

            return b.Max - b.Min;
        }

        public static Vector3 getHalfSize(this BoundingBox b)
        {
            if (b.IsNull())
            {
                return Vector3.Zero;
            }

            if (b.IsInfinite())
            {
                return new Vector3(float.PositiveInfinity);
            }

            return (b.Max - b.Min) * 0.5f;
        }
    }
}
